/*
 ******************************************************************************
 *  Java Signal Framework for routing java objects. 
 *  Copyright (C) 2008  Manuel Stahl (thymythos@web.de)
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public
 *  License as published by the Free Software Foundation; either
 *  version 2 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this library; if not, write to the Free
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 ****************************************************************************** 
 */
package org.tru42.signal.processors.io;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;

import org.tru42.signal.model.AbstractSignal;

public class InputStreamSignal extends AbstractSignal implements Runnable {

	String name;
	
	InputStream stream;
	
	Thread thread;
	
	public InputStreamSignal(Object owner, String name) {
		super(owner);
		this.name = name;
	}
	
	public void setInputStream(InputStream stream) {
		this.stream = stream;
		thread = new Thread(this, "InputStreamSource receive");
		thread.start();
	}
	
	public InputStream getInputStream() {
		return stream;
	}

	public void run() {
		try {
			int read;
			while((read = stream.read()) != -1) {
				setValue(Byte.valueOf((byte) read));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public Thread getThread() {
		return thread;
	}

	@Override
	public String getName() {
		return name;
	}

	@Override
	public Class<?>[] getTypes() {
		return new Class<?>[]{Byte.class};
	}

	@Override
	public void trigger() throws IllegalArgumentException,
			IllegalAccessException, InvocationTargetException {
	}
	
	
}
